<template>
  <div class="mod-user">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.name" placeholder="场次名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button v-if="isAuth('sys:seckill:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button v-if="isAuth('sys:seckill:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      row-key="id"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <!-- <el-table-column  type="expand">
      @expand-change="expandChange"

        <template slot-scope="scope">
          <el-table :data="scope.row.children"
            border
            v-loading="dataListLoading"
          >
              <el-table-column type="selection" width="55"></el-table-column>
              <el-table-column prop="city_name" label="城市"></el-table-column>
              <el-table-column prop="price" label="成本价"></el-table-column>
              <el-table-column prop="sale_price" label="售出价"></el-table-column>
              <el-table-column
                fixed="right"
                header-align="center"
                align="center"
                width="150"
                label="操作">
                <template slot-scope="scope">
                  <el-button v-if="isAuth('sys:user:add')" type="text" size="small" @click="addOrUpdateHandle(scope.row.userId)">新增产品</el-button>
                  <el-button v-if="isAuth('sys:user:update')" type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
                  <el-button v-if="isAuth('sys:user:delete')" type="text" size="small" @click="deleteHandle(scope.row.id, scope.row.sessionName)">删除</el-button>
                </template>
              </el-table-column>
          </el-table>
        </template>
      </el-table-column> -->
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="id"
        header-align="center"
        align="center"
        width="200"
        label="ID">
      </el-table-column>
      <el-table-column
        prop="sessionName"
        header-align="center"
        align="center"
        width="150"
        label="场次名称">
      </el-table-column>
      <el-table-column
        prop="startTime"
        header-align="center"
        align="center"
        width="200"
        label="场次开始时间">
      </el-table-column>
      <el-table-column
        prop="endTime"
        header-align="center"
        align="center"
        width="200"
        label="场次结束时间">
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 0" size="small" type="danger">禁用</el-tag>
          <el-tag v-else size="small">正常</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="sessionStatus"
        header-align="center"
        align="center"
        width="150"
        label="秒杀状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.sessionStatus === '未开始'" size="small" type="danger">未开始</el-tag>
          <el-tag v-if="scope.row.sessionStatus === '正在进行中'" size="small">正在进行中</el-tag>
          <el-tag v-if="scope.row.sessionStatus === '已结束'" size="small" type="info">已结束</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="gmtCreate"
        header-align="center"
        align="center"
        width="180"
        label="创建时间">
      </el-table-column>
      <el-table-column
        prop="gmtModified"
        header-align="center"
        align="center"
        width="180"
        label="修改时间">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button v-if="isAuth('sys:seckill:addproduct') && scope.row.sessionStatus ==='未开始'" type="text" size="small" @click="addProductHandle(scope.row, 'add')">添加秒杀产品</el-button>
          <el-button v-if="isAuth('sys:seckill:addproduct') && scope.row.sessionStatus ==='未开始'" type="text" size="small" @click="addProductHandle(scope.row ,'update')">修改秒杀产品</el-button>
          <!-- <el-button v-if="isAuth('sys:seckill:update') && scope.row.sessionStatus ==='未开始'" type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button> -->
          <el-button v-if="isAuth('sys:seckill:update')" type="text" size="small" @click="addProductHandle(scope.row, 'detail')">详情</el-button>

          <el-button v-if="isAuth('sys:seckill:delete') && scope.row.sessionStatus ==='未开始'" type="text" size="small" @click="deleteHandle(scope.row.id, scope.row.sessionName)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 秒杀活动：新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <!-- 弹窗, 秒杀产品：新增 / 修改 -->
    <add-product v-if="addProductVisible" ref="addProduct" @refreshDataList="getDataList"></add-product>

  </div>
</template>

<script>
import AddOrUpdate from './seckill-add.vue'
import addProduct from './seckill-add-product.vue'
export default {
  data () {
    return {
      dataForm: {
        name: ''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      addProductVisible: false
    }
  },
  components: {
    AddOrUpdate,
    addProduct
  },
  activated () {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList () {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/coupon/coupon/session/list'),
        method: 'get',
        params: this.$http.adornParams({
          'page': this.pageIndex,
          'limit': this.pageSize
        })
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list
          this.dataList.map((item, i) => {
            item.hasChildren = true
          })
          this.totalPage = data.page.totalCount
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle (val) {
      this.dataListSelections = val
    },
    // 秒杀活动：新增 / 修改
    addOrUpdateHandle (id, type) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id, type)
      })
    },
    // 秒杀产品：新增 / 修改
    addProductHandle (row, type) {
      this.addProductVisible = true
      this.$nextTick(() => {
        this.$refs.addProduct.init(row, type)
      })
    },
    // 删除
    deleteHandle (id, sessionName) {
      var sessionNames = sessionName ? [sessionName] : this.dataListSelections.map(item => {
        return item.sessionName
      })
      var ids = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      this.$confirm(`确定对[场次名称=${sessionNames.join(',')}]进行[${sessionNames ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/coupon/coupon/session/delete'),
          method: 'post',
          data: this.$http.adornData(ids, false)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      }).catch(() => {})
    }
    // expandChange(tree, treeNode, resolve) {
    //   setTimeout(() => {
    //     resolve([
    //       {
    //         id: 31,
    //         date: '2016-05-01',
    //         name: '王小虎',
    //         address: '上海市普陀区金沙江路 1519 弄'
    //       }, {
    //         id: 32,
    //         date: '2016-05-01',
    //         name: '王小虎',
    //         address: '上海市普陀区金沙江路 1519 弄'
    //       }
    //     ])
    //   }, 1000)
    // }
  }
}
</script>
